/*
 * JanelaLogin.java
 *
 * Created on 17/04/2011, 02:20:06
 */

package gui;

import java.util.List;
import javax.swing.JOptionPane;
import modelo.Usuario;

/**
 *
 * @author emersonmx
 */
public class JanelaLogin extends javax.swing.JDialog implements UI {

    /** Creates new form JanelaLogin */
    public JanelaLogin(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    rotuloLogin = new javax.swing.JLabel();
    rotuloSenha = new javax.swing.JLabel();
    campoLogin = new javax.swing.JTextField();
    botaoOK = new javax.swing.JButton();
    botaoCancelar = new javax.swing.JButton();
    campoSenha = new javax.swing.JPasswordField();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("Login");
    setResizable(false);
    addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosed(java.awt.event.WindowEvent evt) {
        formWindowClosed(evt);
      }
    });

    rotuloLogin.setText("Login");

    rotuloSenha.setText("Senha");

    botaoOK.setText("OK");
    botaoOK.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        botaoOKActionPerformed(evt);
      }
    });

    botaoCancelar.setText("Cancelar");
    botaoCancelar.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        botaoCancelarActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(rotuloLogin, javax.swing.GroupLayout.Alignment.TRAILING)
              .addComponent(rotuloSenha, javax.swing.GroupLayout.Alignment.TRAILING))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(campoSenha, javax.swing.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE)
              .addComponent(campoLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE)))
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addComponent(botaoOK)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(botaoCancelar)))
        .addContainerGap())
    );

    layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {botaoCancelar, botaoOK});

    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(rotuloLogin)
          .addComponent(campoLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(rotuloSenha)
          .addComponent(campoSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(botaoOK)
          .addComponent(botaoCancelar))
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

    private void botaoCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoCancelarActionPerformed
      System.exit(0);
    }//GEN-LAST:event_botaoCancelarActionPerformed

    private void botaoOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoOKActionPerformed
      Usuario u = new Usuario();

      u.setUsuario(campoLogin.getText());
      u.setSenha(String.valueOf(campoSenha.getPassword()));

      controlador.pesquisar(u);
    }//GEN-LAST:event_botaoOKActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
      System.exit(0);
    }//GEN-LAST:event_formWindowClosed

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton botaoCancelar;
  private javax.swing.JButton botaoOK;
  private javax.swing.JTextField campoLogin;
  private javax.swing.JPasswordField campoSenha;
  private javax.swing.JLabel rotuloLogin;
  private javax.swing.JLabel rotuloSenha;
  // End of variables declaration//GEN-END:variables

  Usuario usuario;
  Controlador controlador;

  @Override
  public Controlador getControlador() { return controlador; }

  @Override
  public void setControlador(Controlador c) { controlador = c; }

  @Override
  public void exibirMensagem(String mensagem) {
    JOptionPane.showMessageDialog(this, mensagem);
  }

  @Override
  public void exibirMensagemErro(String mensagem) {
    JOptionPane.showMessageDialog(this, mensagem, "Erro!", JOptionPane.ERROR_MESSAGE);
  }

  @Override
  public void mostrarObjeto(Object o) {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  @Override
  public void mostrarObjetos(List l) {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public void mostrarPai() {
    setVisible(false);
    getParent().setVisible(true);
  }
}
